//
//  ViewFactory.m
//  DuoBao
//
//  Created by arons on 16/5/6.
//  Copyright © 2016年 aron. All rights reserved.
//

#import "ViewFactory.h"
#import "DBRefreshGifHeader.h"

@interface DBTextField ()<UITextFieldDelegate>

@property (nonatomic, copy) NSString* normalIcon;
@property (nonatomic, copy) NSString* hightLightIcon;

@end

@implementation DBTextField

- (instancetype)initWithFrame:(CGRect)frame
               withNormalIcon:(NSString*)normalIcon
           withHighLightlIcon:(NSString*)hightLightIcon
                    withTFTag:(int)TFTag
           withTFKeyboardType:(UIKeyboardType)keyboardType
                   isTFSecure:(BOOL)isTFSecure
              withPlaceHolder:(NSString*)placeHolder
        withExtBtnNormalImage:(NSString*)extBtnNormalIcon
       withExtBtnSelectdImage:(NSString*)extBtnSelectlIcon
                 extBtnTarget:(id)target
                 extBtnAction:(SEL)extBtnAction {
    if (self = [super initWithFrame:frame]) {
        
        _normalIcon = normalIcon;
        _hightLightIcon = hightLightIcon;
        
        self.layer.cornerRadius = 5;
        self.layer.masksToBounds = YES;
        self.layer.borderColor = [UIColor themeGrayColorWithBorder].CGColor;
        self.layer.borderWidth = 1;
        self.backgroundColor = [UIColor whiteColor];
        
        CGFloat PADDING = 8;

        CGFloat userNameIconWH = 20;
        CGFloat userNameIconY = (CGRectGetHeight(frame) - userNameIconWH)/2;
        UIImageView* userNameIcon = [UIImageView new];
        userNameIcon.contentMode = UIViewContentModeScaleAspectFit;
        userNameIcon.image = [UIImage imageNamed:normalIcon];
        userNameIcon.frame = CGRectMake(PADDING, userNameIconY, userNameIconWH, userNameIconWH);
        [self addSubview:userNameIcon];
        _userNameIcon = userNameIcon;
        
        // 右边按钮
        BOOL shoudAddExtBtn = (extBtnNormalIcon != nil && target != nil && extBtnAction != nil);
        CGFloat extBtnWH = 30;
        if (shoudAddExtBtn) {
            CGFloat extBtnX = CGRectGetWidth(frame) - PADDING - extBtnWH;
            CGFloat extBtnY = (CGRectGetHeight(frame) - extBtnWH)/2;
            UIButton* extBtn = [UIButton new];
            extBtn.frame = CGRectMake(extBtnX, extBtnY, extBtnWH, extBtnWH);
            [extBtn setImage:[UIImage imageNamed:extBtnNormalIcon] forState:UIControlStateNormal];
            [extBtn setImage:[UIImage imageNamed:extBtnSelectlIcon] forState:UIControlStateSelected];
            
            [extBtn addTarget:target action:extBtnAction forControlEvents:UIControlEventTouchUpInside];
            [self addSubview:extBtn];
            _extBtn = extBtn;
        }
        
        // 输入框
        CGFloat userNameTFX = CGRectGetMaxX(userNameIcon.frame) + PADDING;
        CGFloat userNameTFY = 0;
        CGFloat userNameTFH = CGRectGetHeight(frame) - userNameTFY*2;
        CGFloat userNameTFW = shoudAddExtBtn ?
        (CGRectGetWidth(frame) - extBtnWH - PADDING - CGRectGetMaxX(userNameIcon.frame) - PADDING) :
        (CGRectGetWidth(frame) - CGRectGetMaxX(userNameIcon.frame) - PADDING) ;
        UITextField* inputTF = [UITextField new];
        inputTF.frame = CGRectMake(userNameTFX, userNameTFY, userNameTFW, userNameTFH);
        inputTF.backgroundColor = [UIColor whiteColor];//[UIColor whiteColor];
        [inputTF setBorderStyle:UITextBorderStyleNone];
        inputTF.font = [UIFont systemFontOfSize:14];
        inputTF.placeholder = placeHolder;
        inputTF.textColor = [UIColor themeGrayColorWithContent];
        inputTF.keyboardType = keyboardType;
        inputTF.secureTextEntry = isTFSecure;
        inputTF.clearButtonMode = UITextFieldViewModeWhileEditing;
        inputTF.tag = TFTag;
        inputTF.delegate = self;
        [self addSubview:inputTF];
        _inputTF = inputTF;

    }
    return self;
}

- (void)textFieldDidBeginEditing:(UITextField *)textField {
    _userNameIcon.image = [UIImage imageNamed:_hightLightIcon];
}

- (void)textFieldDidEndEditing:(UITextField *)textField {
    _userNameIcon.image = [UIImage imageNamed:_normalIcon];
}

@end

@implementation ViewFactory

+ (DBRefreshGifHeader*)generateRefreshGifHeaderWithTarget:(id)tagerget refreshingAction:(SEL)action{
    //设置下拉动画图片
    NSMutableArray *pullImages = [NSMutableArray array];
//    for (NSUInteger i = 1; i<=60; ++i) {
//        UIImage *image = [UIImage imageNamed:[NSString stringWithFormat:@"dropdown_anim__000%zd",i]];
//        [pullImages addObject:image];
//    }
    for (NSUInteger i = 1; i<=3; ++i) {
        UIImage *image = [UIImage imageNamed:[NSString stringWithFormat:@"db_loading_000%zd",i]];
        [pullImages addObject:image];
    }
    
    //设置刷新动画图片 动效_00047
    NSMutableArray *refreshingImages = [NSMutableArray array];
//    for (NSInteger i = 1; i<=3; i++) {
//        UIImage *image = [UIImage imageNamed:[NSString stringWithFormat:@"dropdown_loading_0%zd",i]];
//        [refreshingImages addObject:image];
//    }
    for (NSInteger i = 47; i<=72; i+=2) {
        UIImage *image = [UIImage imageNamed:[NSString stringWithFormat:@"动效_000%zd",i]];
        [refreshingImages addObject:image];
    }
    
    // 设置回调（一旦进入刷新状态，就调用target的action，也就是调用self的loadNewData方法）
    DBRefreshGifHeader *header = [DBRefreshGifHeader headerWithRefreshingTarget:tagerget refreshingAction:action];
    // 设置普通状态的动画图片
    [header setImages:pullImages forState:MJRefreshStateIdle];
    // 设置即将刷新状态的动画图片（一松开就会刷新的状态）
    [header setImages:refreshingImages forState:MJRefreshStatePulling];
    // 设置正在刷新状态的动画图片
    [header setImages:refreshingImages forState:MJRefreshStateRefreshing];
    
    return header;
}

+ (DBTextField*)generateTextFieldViewWithFrame:(CGRect)containerFrame
                                withNormalIcon:(NSString*)normalIcon
                            withHighLightlIcon:(NSString*)hightLightIcon
                                withTFTag:(int)TFTag
                       withTFKeyboardType:(UIKeyboardType)keyboardType
                               isTFSecure:(BOOL)isTFSecure
                          withPlaceHolder:(NSString*)placeHolder
                    withExtBtnNormalImage:(NSString*)extBtnNormalIcon
                   withExtBtnSelectdImage:(NSString*)extBtnSelectlIcon
                             extBtnTarget:(id)target
                             extBtnAction:(SEL)extBtnAction {
    
    /*
    CGFloat PADDING = 8;
    
    UIView* userNameView = [UIView new];
    userNameView.layer.cornerRadius = 5;
    userNameView.layer.masksToBounds = YES;
    userNameView.layer.borderColor = [UIColor themeGrayColorWithBorder].CGColor;
    userNameView.layer.borderWidth = 1;
    userNameView.backgroundColor = [UIColor whiteColor];
    userNameView.frame = containerFrame;
    
    // 左边图标
    CGFloat userNameIconWH = 20;
    CGFloat userNameIconY = (CGRectGetHeight(userNameView.frame) - userNameIconWH)/2;
    UIImageView* userNameIcon = [UIImageView new];
    userNameIcon.contentMode = UIViewContentModeScaleAspectFit;
    userNameIcon.image = [UIImage imageNamed:icon];
    userNameIcon.frame = CGRectMake(PADDING, userNameIconY, userNameIconWH, userNameIconWH);
    [userNameView addSubview:userNameIcon];
    
    // 右边按钮
    BOOL shoudAddExtBtn = (extBtnNormalIcon != nil && target != nil && extBtnAction != nil);
    CGFloat extBtnWH = 30;
    if (shoudAddExtBtn) {
        CGFloat extBtnX = CGRectGetWidth(userNameView.frame) - PADDING - extBtnWH;
        CGFloat extBtnY = (CGRectGetHeight(userNameView.frame) - extBtnWH)/2;
        UIButton* extBtn = [UIButton new];
        extBtn.frame = CGRectMake(extBtnX, extBtnY, extBtnWH, extBtnWH);
        [extBtn setImage:[UIImage imageNamed:extBtnNormalIcon] forState:UIControlStateNormal];
        [extBtn setImage:[UIImage imageNamed:extBtnSelectlIcon] forState:UIControlStateSelected];
        
        [extBtn addTarget:target action:extBtnAction forControlEvents:UIControlEventTouchUpInside];
        [userNameView addSubview:extBtn];
    }
    
    // 输入框
    CGFloat userNameTFX = CGRectGetMaxX(userNameIcon.frame) + PADDING;
    CGFloat userNameTFY = 0;
    CGFloat userNameTFH = CGRectGetHeight(userNameView.frame) - userNameTFY*2;
    CGFloat userNameTFW = shoudAddExtBtn ?
    (CGRectGetWidth(userNameView.frame) - extBtnWH - PADDING - CGRectGetMaxX(userNameIcon.frame) - PADDING) :
    (CGRectGetWidth(userNameView.frame) - CGRectGetMaxX(userNameIcon.frame) - PADDING) ;
    UITextField* userNameTF = [UITextField new];
    userNameTF.frame = CGRectMake(userNameTFX, userNameTFY, userNameTFW, userNameTFH);
    userNameTF.backgroundColor = [UIColor whiteColor];//[UIColor whiteColor];
    [userNameTF setBorderStyle:UITextBorderStyleNone];
    userNameTF.font = [UIFont systemFontOfSize:14];
    userNameTF.placeholder = placeHolder;
    userNameTF.textColor = [UIColor themeGrayColorWithContent];
    userNameTF.keyboardType = keyboardType;
    userNameTF.secureTextEntry = isTFSecure;
    userNameTF.clearButtonMode = UITextFieldViewModeWhileEditing;
    userNameTF.tag = TFTag;
    userNameTF.delegate = self;
    [userNameView addSubview:userNameTF];
    
    return userNameView;
     */
    
    return [[DBTextField alloc]
            initWithFrame:containerFrame
            withNormalIcon:normalIcon
            withHighLightlIcon:hightLightIcon
            withTFTag:TFTag
            withTFKeyboardType:keyboardType
            isTFSecure:isTFSecure
            withPlaceHolder:placeHolder
            withExtBtnNormalImage:extBtnNormalIcon
            withExtBtnSelectdImage:extBtnSelectlIcon
            extBtnTarget:target extBtnAction:extBtnAction];
}

@end
